package me.khrystal.rxnews.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

/**
 * @FileName: me.khrystal.rxnews.utils.BitmapUtil.java
 * @Fuction: Image|Bitmap Util
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-03-03 15:18
 * @UpdateUser:
 * @UpdateDate:
 */
public class BitmapUtil {


    /**
     * viewshoot to bitmap
     * @param v
     * @return
     */
    public static Bitmap createViewBitmap(View v){
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth()
                ,v.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

    /**
     * viewshoot from cache to bitmap
     * @param view
     * @return
     */
    public static Bitmap converViewToBitmap(View view){
        view.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
        view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
        view.buildDrawingCache();
        return view.getDrawingCache();
    }

    /**
     * get image size
     * notice options.inJustDecodeBounds = true;
     * @param context
     * @param id
     * @return
     */
    public static int[] getImageRealSize(Context context,int id){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id,options);
        int size[] = new int[2];
        size[0] = options.outWidth;
        size[1] = options.outHeight;
        return size;
    }

    /**
     * zip ratio getInSampleSize
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth,int reqHeight){
        //original image size
        final int height = options.outHeight;
        final int width  = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth){
            final int heightRatio = Math.round((float)height/(float)reqHeight);
            final int widthRatio = Math.round((float)width/(float)reqWidth);

            inSampleSize = heightRatio<widthRatio?heightRatio:widthRatio;
        }

        return inSampleSize;
    }

    /**
     * decode Bitmap from resource file
     * @param res
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId,
                                                         int reqWidth,int reqHeight){
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res,resId,options);
        options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res,resId,options);
    }

}
